package com.woniuxy.joylife.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.common.constant.ManagerConstant;
import com.woniuxy.common.entity.CenterUser;
import com.woniuxy.common.mapper.CenterUserMapper;
import com.woniuxy.common.utils.KeyUtil;
import com.woniuxy.joylife.manager.VO.AuthVO;
import com.woniuxy.joylife.manager.dao.OwnerDao;
import com.woniuxy.joylife.manager.dao.OwnerUserDao;
import com.woniuxy.joylife.manager.entity.OwnerEntity;
import com.woniuxy.joylife.manager.entity.OwnerUserEntity;
import com.woniuxy.joylife.manager.service.OwnerService;
import com.woniuxy.joylife.manager.utils.PageUtils;
import com.woniuxy.joylife.manager.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service("ownerService")
public class OwnerServiceImpl extends ServiceImpl<OwnerDao, OwnerEntity> implements OwnerService {

    @Autowired
    OwnerUserDao ownerUserDao;

    @Autowired
    CenterUserMapper centerUserMapper;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OwnerEntity> page = this.page(
                new Query<OwnerEntity>().getPage(params),
                new QueryWrapper<OwnerEntity>().eq("state",ManagerConstant.UNDELETED)
        );

        return new PageUtils(page);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Boolean checkExist(AuthVO authVO) {
        // 业主信息校验
        OwnerEntity ownerEntity = baseMapper.selectOne(new QueryWrapper<OwnerEntity>()
                .eq("name", authVO.getName())
                .eq("phone", authVO.getPhone())
                .eq("id_card", authVO.getCenterCridentialCode())
                .eq("state",ManagerConstant.UNDELETED));
        if (ownerEntity==null){
            return false;
        }else {
            // 账户信息不存在
            CenterUser centerUser = centerUserMapper.selectByPrimaryKey(authVO.getId());
            if (centerUser ==null){
                return false;
            }
            OwnerUserEntity ownerUserEntity = new OwnerUserEntity();
            ownerUserEntity.setId(KeyUtil.generateKey());
            ownerUserEntity.setUserId(centerUser.getId());
            ownerUserEntity.setManagerId(null);
            // 存入物业用户表
            ownerUserDao.insert(ownerUserEntity);
            return true;
        }
    }

    @Override
    public Boolean saveOwnerEntity(OwnerEntity ownerEntity) {
        // 插入业主
        try{
            baseMapper.insert(ownerEntity);
            OwnerUserEntity ownerUserEntity = new OwnerUserEntity();
            ownerUserEntity.setId(KeyUtil.generateKey());
            ownerUserEntity.setOwnerId(ownerEntity.getOwnerId());
            ownerUserEntity.setManagerId(null);
            // 绑定业主物业
            ownerUserDao.insert(ownerUserEntity);
        }catch (Exception e){
            return false;
        }
        return true;
    }

}